package conson.tech.manage.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;

/**
 * InterfaceName: TimeUtils
 * Description: 时间工具类
 * Author: XYF
 * Date: 2020/7/31 13:28
 * Version: 1.0
 */
public class TimeUtils {
    /**
     * 20位末尾的数字id
     */
    public static volatile int Guid = 100;


    /**
     * 获取某天凌晨后的整点时间 2020-09-10 08:00:00
     *
     * @param hour
     * @return LocalDateTime
     */
    public static LocalDateTime getTodayPlusHour(LocalDate time, Integer hour) {
        LocalDateTime todayStart = LocalDateTime.of(time, LocalTime.MIN);
        return todayStart.plusHours(hour).toLocalDate().atTime(todayStart.plusHours(hour).getHour(), 0, 0);
    }

    /**
     * 将String 日期转换成LocalDateTime
     *
     * @param date
     * @return java.time.LocalDateTime
     */
    public static LocalDateTime dateToLocalDateTime(String date) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        //字符转localDateTime
        return LocalDateTime.parse(date, dateTimeFormatter);
    }

    /**
     * LocalTime to Date
     *
     * @param localTime
     * @return java.util.Date
     */
    public static Date localTimeToDate(LocalTime localTime) {
        LocalDate localDate = LocalDate.now();
        LocalDateTime localDateTime = LocalDateTime.of(localDate, localTime);
        ZoneId zone = ZoneId.systemDefault();
        Instant instant = localDateTime.atZone(zone).toInstant();
        Date date = Date.from(instant);
        return date;
    }

    /**
     * LocalDateTime to String 类型的Date
     *
     * @param localDateTime
     * @return java.lang.String
     */
    public static String localDateTimeToString(LocalDateTime localDateTime) throws ParseException {
        //当天零点
        LocalDateTime today_start = LocalDateTime.of(localDateTime.toLocalDate(), LocalTime.MIN);
        return today_start.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
    }

    /**
     * 生成20位的优惠券码(年份+时间戳去除两位+3位的随机码)
     *
     * @param
     * @return java.lang.String
     */
    public static String getGuid() {
        TimeUtils.Guid += 1;

        long now = System.currentTimeMillis();
        //获取4位年份数字
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");
        //获取时间戳
        String time = dateFormat.format(now);
        String info = now + "";
        //获取三位随机数
        //int ran=(int) ((Math.random()*9+1)*100);
        //要是一段时间内的数据连过大会有重复的情况，所以做以下修改
        int ran = 0;
        if (TimeUtils.Guid > 999) {
            TimeUtils.Guid = 100;
        }
        ran = TimeUtils.Guid;

        return time + info.substring(2, info.length()) + ran;
    }

    /**
     * 获取上个月的年份+月份 20207 202010 202012
     *
     * @return java.lang.String
     */
    public static String getLastMonth() {
        LocalDate localDate = LocalDateTime.now().toLocalDate().minusMonths(1);
        return String.valueOf(localDate.getYear()) + localDate.getMonth().getValue();
    }


    /**
     * 获取上个月第一天
     *
     * @return java.time.LocalDateTime
     */
    public static LocalDateTime getlastMonthFirstDay() {
        LocalDateTime date1 = LocalDateTime.now().with(TemporalAdjusters.firstDayOfMonth());
        LocalDateTime of = LocalDateTime.of(date1.toLocalDate(), LocalTime.MIN);
        return of.minusMonths(1);
    }

    /**
     * 获取上个月最后一天
     *
     * @return java.time.LocalDateTime
     */
    public static LocalDateTime getlastMonthLastDay() {
        LocalDateTime date2 = LocalDateTime.now().with(TemporalAdjusters.lastDayOfMonth());
        LocalDateTime of1 = LocalDateTime.of(date2.toLocalDate(), LocalTime.MAX);
        return of1.minusMonths(1);
    }

}
